package com.mrjy.weixin.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.entity.WeixinAccount;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.weixin.bo.IWeixinAccountBo;
import com.mrjy.wechat.process.MpAccount;
import com.mrjy.wechat.process.WxMemoryCacheClient;

@Controller
@RequestMapping("/weixin/weixinAccount")
public class WeixinAccountController {
	@Resource
	private IWeixinAccountBo weiXinAccountBo;
	
	/**
	 * 根据学校ID查询账户信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/queryBySchoolId")
	@ResponseBody
	public Object queryBySchoolId( HttpSession session,HttpServletRequest request ) {
		UserInfo userInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		if(userInfo == null){
			return null;
		}
		if(userInfo.getSchoolId() == null){
			userInfo.setSchoolId(1);
		}
		return weiXinAccountBo.queryBySchoolId( userInfo.getSchoolId() );
	}
	
	/**
	 * 账户唯一验证
	 * @param session
	 * @param request
	 * @param weixinAccount
	 * @return
	 */
	@RequestMapping("/accountValidate")
	@ResponseBody
	public Object accountValidate( HttpSession session,HttpServletRequest request,WeixinAccount weixinAccount ) {
		UserInfo userInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		if(userInfo == null){
			return "error";
		}
		weixinAccount.setSchoolId(userInfo.getSchoolId());
		return weiXinAccountBo.accountValidate( weixinAccount  );
	}
	
	/**
	 * 新增
	 * @param session
	 * @param request
	 * @param weixinAccount
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Object addWeixinAccount( HttpSession session,HttpServletRequest request,WeixinAccount weixinAccount ) {
		UserInfo userInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		if(userInfo == null){
			return "uniqueValue_公众号";
		}
		if(userInfo.getSchoolId() == null){
			weixinAccount.setSchoolId(1);
		}else{
			weixinAccount.setSchoolId(userInfo.getSchoolId());
		}
		weixinAccount.setUrl("");
		int i  = 0;
		try {
			i = weiXinAccountBo.weixinAccount( weixinAccount  ) ;
		} catch (Exception e) {
			i = weiXinAccountBo.updateBySchoolId( weixinAccount );
		}
		
		if( i > 0){
			MpAccount a = new MpAccount();
			a.setAccount(weixinAccount.getAccount());
			a.setAppid(weixinAccount.getAppid());
			a.setAppsecret(weixinAccount.getAppsecret());
			a.setMsgcount(weixinAccount.getMsgCount());
			a.setToken(weixinAccount.getToken());
			a.setUrl(weixinAccount.getUrl());
			WxMemoryCacheClient.addMpAccount(a);
			return "ok";
		}
		return "uniqueValue_公众号";
	}
	
	/**
	 * 修改
	 * @param session
	 * @param request
	 * @param weixinAccount
	 * @return
	 */
	@RequestMapping("/updateById")
	@ResponseBody
	public Object updateById( HttpSession session,HttpServletRequest request,WeixinAccount weixinAccount ) {
		UserInfo userInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		if( userInfo == null ){
			return "uniqueValue_公众号";
		}
		if(weiXinAccountBo.updateById( weixinAccount  ) > 0){
			MpAccount a = new MpAccount();
			a.setAccount(weixinAccount.getAccount());
			a.setAppid(weixinAccount.getAppid());
			a.setAppsecret(weixinAccount.getAppsecret());
			a.setMsgcount(weixinAccount.getMsgCount());
			a.setToken(weixinAccount.getToken());
			a.setUrl(weixinAccount.getUrl());
			WxMemoryCacheClient.addMpAccount(a);
			return "ok";
		}
		return "uniqueValue_公众号";
	}
	
}
